/*
 * Copyright 2010 Alessio Stalla.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dynaspring;

import java.util.Map;

import org.armedbear.lisp.JavaObject;
import org.armedbear.lisp.LispObject;
import org.springframework.beans.factory.config.AbstractFactoryBean;

/**
 * Factory bean implementation that invokes a Lisp function to create the object. This factory bean can receive a map of dependencies that can
 * be accessed by the function. Dependencies are resolved by Spring when this class is instantiated by the context, so they can be references
 * to other beans in the context.
 * @author Alessio Stalla
 *
 */
public class LispFactoryBean extends AbstractFactoryBean<Object> {

	private Class<?> objectType;
	private LispObject closure;
	private Map<Object, Object> dependencies;
	
	public LispFactoryBean(LispObject closure, Class<?> objectType) {
		this.objectType = objectType;
		this.closure = closure;
	}
	
	@Override
	protected Object createInstance() throws Exception {
		return closure.execute(JavaObject.getInstance(this)).javaInstance();
	}

	@Override
	public Class<?> getObjectType() {
		return objectType;
	}
	
	public Map<Object, Object> getDependencies() {
		return dependencies;
	}
	
	public void setDependencies(Map<Object, Object> dependencies) {
		this.dependencies = dependencies;
	}
	
	public Object getDependency(Object name) {
		return dependencies.get(name);
	}

}
